import axios from 'axios';
import { message } from 'antd';

import qs from 'querystring'

const request = axios.create({
    baseURL: 'http://xawn.f3322.net:8012',
    timeout: 5000,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
    }

});


request.interceptors.response.use(res => {
    return res

}, (err) => {
    const response = err.response
    switch (response.status) {
        case 401:
            message.error('身份过期，请重新认证')
            localStorage.removeItem('token')
            localStorage.removeItem('userInfo')
            location.href = 'http://127.0.0.1:5173/'
            break;
        case 500: message.error('服务器内部错误')
            break
        case 404: message.error('请求错误')
            break
        default:
            break
    }
})

request.interceptors.request.use((req) => {
    const qa = qs.stringify(req.data)
    if (localStorage.token) {
        req.headers['Content-Type'] = 'application/x-www-form-urlencoded';
        req.headers.Authentication = localStorage.getItem('token');
    }
    req.data = qa
    return req;

}, (err) => {
    // console.log('ree :>> ');
    return Promise.reject(err);
})
export default request;